import redis.clients.jedis.Jedis;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class LoginFrame extends JFrame {
    private JTextField usernameField;
    private JPasswordField passwordField;
    private JButton loginButton;

    public LoginFrame() {
        // 设置布局
        setSize(300,200);
        setLayout(new GridLayout(3, 2));

        // 创建用户名和密码输入框
        JLabel usernameLabel = new JLabel("用户名:");
        usernameField = new JTextField();
        JLabel passwordLabel = new JLabel("密码:");
        passwordField = new JPasswordField();

        // 创建登录按钮
        loginButton = new JButton("登录");

        // 添加控件到界面
        add(usernameLabel);
        add(usernameField);
        add(passwordLabel);
        add(passwordField);
        add(new JLabel());
        add(loginButton);

        // 绑定登录按钮的点击事件
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = usernameField.getText();
                String password = new String(passwordField.getPassword());

                if (checkRedisLogin(username)) {
                    // Redis中已登录，直接跳转到DormitoryManagementSystem界面
                    openDormitoryManagementSystem(username);
                } else {
                    // 查询数据库进行登录验证
                    if (checkDatabaseLogin(username, password)) {
                        // 登录成功，显示登录成功提示，并将登录状态存储到Redis
                        JOptionPane.showMessageDialog(LoginFrame.this, "登录成功");
                        setRedisLogin(username);
                        openDormitoryManagementSystem(username);
                    } else {
                        // 登录失败，显示错误提示
                        JOptionPane.showMessageDialog(LoginFrame.this, "用户名或密码错误");
                    }
                }
            }
        });
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    private boolean checkRedisLogin(String username) {
        // 连接到 Redis
        Jedis jedis = new Jedis("localhost", 6379);
        
        // 查询 Redis 中指定用户名的登录状态
        String value = jedis.get(username);

        // 关闭 Redis 连接
        jedis.close();

        return value != null && value.equals("1");
    }

    private void setRedisLogin(String username) {
        // 连接到 Redis
        Jedis jedis = new Jedis("localhost", 6379);
        
        // 设置指定用户名的登录状态为 1
        jedis.set(username, "1");

        // 关闭 Redis 连接
        jedis.close();
    }

    private boolean checkDatabaseLogin(String username, String password) {
        // 使用 JDBC 连接数据库
        Connection connection = JdbcConnection.getConnection();
        if (connection == null) {
            return false;
        }

        try {
            // 执行 SQL 查询
            PreparedStatement statement = connection.prepareStatement("SELECT * FROM users WHERE username = ? AND passwd = ?");
            statement.setString(1, username);
            statement.setString(2, password);
            ResultSet resultSet = statement.executeQuery();

            // 如果查询结果有数据，则登录验证通过
            if (resultSet.next()) {
                resultSet.close();
                statement.close();
                connection.close();
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return false;
    }

    private void openDormitoryManagementSystem(String username) {
        DormitoryManagementSystem dormManagementSystem = new DormitoryManagementSystem();
        dispose();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new LoginFrame();
            }
        });
    }
}


